# --- INTRO

cmake_minimum_required( VERSION 2.8 )

project( ALL )

# The following is not portable! :-(
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++0x" )




# --- TESTING
# Based on http://code.google.com/p/clementine-player/source/browse/tests/CMakeLists.txt

find_package( Threads )

find_path( GMOCK_INCLUDE_DIRS gmock/gmock.h )
find_library( GMOCK_LIBRARIES gmock )
find_path( GTEST_INCLUDE_DIRS gtest/gtest.h )
find_library( GTEST_LIBRARIES gtest )

include_directories( ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS} )

add_library( TestMain STATIC EXCLUDE_FROM_ALL test/main.cpp )
target_link_libraries( TestMain ${GMOCK_LIBRARIES} ${GTEST_LIBRARIES} pthread EngineCore MusicCore )

add_custom_target( test
    COMMENT "Done testing"
)
add_custom_target( run_tests
    COMMENT "Running tests"
)
add_custom_target( build_tests
    COMMENT "Done building tests"
)
add_dependencies( test run_tests )
add_dependencies( run_tests build_tests )

set_property( GLOBAL PROPERTY LIST_OF_TESTS_TO_RUN )

# Given a file foo_test.cpp, creates a target foo_test and adds it to the test target.
macro( add_test_file test_source )
    get_filename_component( TEST_NAME ${test_source} NAME_WE )

    add_executable( ${TEST_NAME}
        EXCLUDE_FROM_ALL
        ${test_source}
    )

    link_directories( ${ALL_BINARY_DIR}/Engine/Core
        ${ALL_BINARY_DIR}/Music/Core
    )
    target_link_libraries( ${TEST_NAME}
        TestMain EngineCore MusicCore
    )

    set_property( GLOBAL APPEND PROPERTY LIST_OF_TESTS_TO_RUN "${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME}${CMAKE_EXECUTABLE_SUFFIX}" )
    add_dependencies( build_tests ${TEST_NAME} )
endmacro( add_test_file )




# --- MODULES

add_subdirectory( Engine )
add_subdirectory( Music )

add_dependencies( test MusicGuessing )

get_property( TESTS_TO_RUN GLOBAL PROPERTY LIST_OF_TESTS_TO_RUN )
foreach( SINGLE_TEST ${TESTS_TO_RUN} )
    add_custom_command( TARGET run_tests POST_BUILD COMMAND ${SINGLE_TEST} )
endforeach( SINGLE_TEST )
